<?php
class Bib_Acl extends Zend_Acl {
	public function __construct() {
		
		$config = Zend_Registry::get('config');
		
		/*
		 * Add resources
		 */
		$resources = $config->acl->resource;
		if ($resources instanceof Zend_Config) {
			foreach ($resources as $res) {
				$this->add(new Zend_Acl_Resource($res));
			} 
		}
		
		/*
		 * Add roles 
		 */
		$roles = $config->acl->role;
		foreach ($roles as $roleName => $roleValue) {
			
			if ($roleValue instanceof Zend_Config) {
				$this->addRole(new Zend_Acl_Role($roleName), $roleValue->parent);
			} else {
				$this->addRole(new Zend_Acl_Role($roleName));
			}
		}
		
		/*
		 * Add permissions
		 */
		$permissions = $config->acl->permission;
		foreach ($permissions as $perm) {
			$roleName = $perm->role;
			$res = $perm->resource;
			$act = $perm->action == 'ALL' ? null : $perm->action;
			
			if($perm->type == 'allow') {
				$this->allow($roleName, $res, $act);
			}
			else {
				$this->deny($roleName, $res, $act);
			}
			
		}
		
	}
	
}